package leecode;

/**
 * @author csp 2024-06-26
 */
public class Question121 {

    public static void main(String[] args) {
        Question121 question121 = new Question121();
        System.out.println(question121.maxProfit(new int[]{7,1,5,3,6,4}));
    }

    public int maxProfit(int[] prices) {
        return dp(prices);
    }

    /**
     * 动态规划
     *
     **/
    public int dp(int[] prices) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice) {
                minprice = prices[i];
            } else if (maxprofit < prices[i] - minprice) {
                maxprofit = prices[i] - minprice;
            }
        }
        return maxprofit;
    }

}
